# a = 1
#
# def foo():
#     # global a
#     a = 2
#     print(a)
#
# foo()
# print(a)

# a = 1
# b = []
#
# def foo():
#     a = 2
#     b=8
#     # b.append(2)
#     # 局部变量会在函数声明的时候就定义好
#     # 不是按照我们逻辑思维上先执行全局变量b.append(2), 然后再声明一个局部变量b
#     # 而是再函数声明之初就已经定义了b为局部变量
#     # b = 3
#     return None
#
# foo()
# print(a)
# print(b)

#自由变量，闭包！！！！！！！！
def make_averager():
    series = []
    def averager(value):
        # nonlocal series
        series.append(value)   #怎么形容serious，serious是自由变量
        total = sum(series)
        return total / len(series)		#总数/长度
    return averager  #返回一个函数

# my_avg就是延申了作用域的函数
# series就是被延申作用域的变量，自由变量
my_avg = make_averager()
print(my_avg(1))
print(my_avg(2))
print(my_avg(3))


# !!!!!!!!!!!!!!!!!!!!!!
# def make_averager():
#     total=0
#     count=0
#     def averager(value):
#         total += value
#         count += 1
#         return total / count	#总数/长度
#     return averager  #返回一个函数
# # UnboundLocalError: local variable 'total' referenced before assignment
# # my_avg就是延申了作用域的函数
# # series就是被延申作用域的变量，自由变量
# my_avg = make_averager()
# print(my_avg(1))
# print(my_avg(2))
# print(my_avg(3))


# ！！！！！
# def make_averager():
#     total=0
#     count=0
#     def averager(value):
#         nonlocal total,count
#         total += value
#         count += 1
#         return total / count	#总数/长度
#     return averager  #返回一个函数
# # UnboundLocalError: local variable 'total' referenced before assignment
# # my_avg就是延申了作用域的函数
# # series就是被延申作用域的变量，自由变量
# my_avg = make_averager()
# print(my_avg(1))
# print(my_avg(2))
# print(my_avg(3))